package com.zhaungjie.base.utils.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * json工具
 * 存在无法序列化LocalDateXXX的java8 time
 * @author zhuangjie
 * @date 2023/11/29
 */

public class JsonForJSUtils {

    private static final ObjectMapper objectMapper = ObjectMapperBuilder.ReadyMade.FOR_JS_OM;

    /**
     * json 序列化
     *
     * @param object 对象
     * @return {@link String}
     */
    public static String stringify(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JsonForJSUtils::stringify 异常！");
        }
    }

    /**
     * json转指定对象
     *
     * @param jsonStr json-str
     * @param clazz   clazz
     * @return {@link T}
     */
    public static <T> T parse(String jsonStr, Class<T> clazz) {
        try {
            return objectMapper.readValue(jsonStr, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JsonForJSUtils::parse 异常！");
        }
    }

}
